package com.gzz.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzz.common.R;
import com.gzz.pojo.ImgsResp;
import com.gzz.pojo.Strategy;
import com.gzz.pojo.User;
import com.gzz.service.ImgsRespService;
import com.gzz.service.OssUploadService;
import com.gzz.service.StrategyService;
import com.gzz.service.UserService;
import com.gzz.utils.JWTUtils;
import com.gzz.utils.OssUtils;
import com.gzz.vo.StrategyDetailVo;
import com.gzz.vo.StrategyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.gzz.utils.OssUtils.StrategyTypeId;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-11-20
 */
@RestController
@RequestMapping("/api/strategy")
public class StrategyController {

	@Autowired
	OssUploadService ossUploadService;
	@Autowired
	ImgsRespService imgsRespService;
	@Autowired
	UserService userService;

	@Autowired
	StrategyService strategyService;


//	@GetMapping("/comment")
//	{
//		public R<>
//	}
	@GetMapping("/list")
	public R<Page> getlist(String keyword, int pageNo, int pageSize){
		return strategyService.getListByTitle(keyword,pageNo,pageSize);
	}

	@GetMapping("/detail")
	public R<StrategyDetailVo> getStrategyDetail(Long stId){
		return strategyService.getStrategyDetail(stId);
	}
	@PostMapping("/upload")
	public R<String> UploadImg(String type,@RequestParam("file") MultipartFile multipartFile){
		String imgUrl = ossUploadService.uploadFile(multipartFile,type);
		if(imgUrl!="error"){
			return R.success(imgUrl);
		}
		return R.error(imgUrl);
	}
	@GetMapping("/del")
	public R<String>delbystId(HttpServletRequest request,HttpServletResponse response,Long stId){
		String token = request.getHeader("token");
		//获取用户id
		Long id = JWTUtils.parseToken(token);
		User user = userService.getById(id);

		if(user==null){
			response.setStatus(401);
			return R.error("请登录");
		}

		boolean row = strategyService.removeById(stId);

		boolean f = imgsRespService.removeByIdAndTypeId(stId,StrategyTypeId);
		if(f&&row){
			return R.success("成功");
		}
		else{
			return R.error("删除失败");
		}
	}

	@GetMapping("/myList")
	public R<List<Strategy>> getMyAll(HttpServletRequest request,HttpServletResponse response){
		String token = request.getHeader("token");
		//获取用户id
		Long id = JWTUtils.parseToken(token);
		User user = userService.getById(id);

		if(user==null){
			response.setStatus(401);
			return R.error("请登录");
		}


		List<Strategy> list = strategyService.getListByUserId(id);

		if(list==null){
			return R.error("暂无数据");
		}
		return  R.success(list);
	}

	@PostMapping("/add")
	public R<String> AddSt(HttpServletRequest request, HttpServletResponse response, @RequestBody StrategyVo article) throws UnsupportedEncodingException {
		String token = request.getHeader("token");
		//获取用户id
		Long id = JWTUtils.parseToken(token);
		User user = userService.getById(id);
		if(user==null){
			response.setStatus(401);
			return R.error("请登录");
		}
		Long stId = (long) Math.abs(UUID.randomUUID().hashCode());

		//存入攻略表
		String title = article.getStTitle();
		String context = article.getContext();
		String coverImage = article.getStCoverImage();
		Strategy strategy = new Strategy();
		strategy.setUserId(id);
		strategy.setContext(context);
		strategy.setCreateTime(new Date());
		strategy.setUpdateTime(new Date());
		strategy.setStCoverImage(coverImage);
		strategy.setStId(stId);
		strategy.setStTitle(title);


		boolean row = strategyService.save(strategy);
		if(!row){
			return R.error("数据异常");
		}
		//存入图片仓库

		List<String> imgs = article.getDetailImgs();

		Integer typeId = StrategyTypeId;


		boolean r = imgsRespService.saveList(imgs,stId,typeId);
		if(!r){
			return R.error("数据异常");
		}
		return  R.success("成功分享攻略");
	}


}

